package com.xonro.common.histrans.sfeg.biz.seal;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class SealApplyBiz extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
       RowMap yzData= SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMap("SELECT * FROM yz_info  WHERE id = ?  ",mainData.getString("yzid"));
        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_SEAL_APPLY");//项目基础信息
        logModel.setOuterTable("tb_SealUseManage");//基础项目信息表
        logModel.setOuterPK(mainData.getString("suCode"));
        logModel.setOuterExt1(mainData.getString("sealCode"));
        logModel.setOuterExt2(mainData.getString("yzlx").toUpperCase());//印章类型
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setBoName("BO_XR_SEAL_APPLY");
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_NO", mainData.getString("suCode"));//用印编号
        mainBO.set("REASON", mainData.getString("Reason"));//用印事由
        mainBO.set("REMARK", mainData.getString("bz"));//备注
        mainBO.set("APPLY_DEPT_ID", mainData.getString("fillDept"));//部门
        mainBO.set("SEAL_TYPE", yzData.getString("lx").toUpperCase());//印章类型
        mainBO.set("SEAL_NAME", yzData.getString("mc"));//印章名称
        mainBO.set("SEAL_LEVEL", yzData.getString("jb"));//印章级别
        mainBO.set("PROJECT_NO", yzData.getString("xmbh"));//项目编号

        List<RowMap> sonDatas= SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_SealUseManageD  WHERE suCode = ?  ",mainData.getString("suCode"));
        for(RowMap son:sonDatas){
            String uuid = UUIDGener.getUUID();
            BOCreateData subBOCreateData = new BOCreateData();
            boCreateDataList.add(subBOCreateData);
            subBOCreateData.setCreateUser(createUser);
            subBOCreateData.setBoName("BO_XR_SEAL_APPLY_LIST");
            BO subBO = new BO();
            subBO.setId(uuid);
            subBOCreateData.setBo(subBO);
            subBO.set("FILENAME",son.getString("wjmc"));
            subBO.set("COUNT",son.getString("fs"));
            super.addLogModel(historyTransContext, "tb_SealUseManageD", son.getString("wjmc"), "BO_XR_SEAL_APPLY_LIST", uuid, bindId);
            FileTransData fileData = new FileTransData();
            fileData.setBoId(uuid);
            //印章id
            fileData.setFkCode(mainData.getString("suCode"));
            fileData.setProcessInstId(bindId);
            historyTransContext.getFileTransDatas().add(fileData);
            fileData.setBoName("BO_XR_SEAL_APPLY_LIST");
            fileData.setFieldName("ATTACHMENT");//
        }
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);

  /*      //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //印章id
        fileTransData.setFkCode(mainData.getString("suCode"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_SEAL_APPLY");
        fileTransData.setBoName("BO_XR_SEAL_APPLY");
        fileTransData.setFieldName("ATTACHMENT");//印张图片*/


    }


    @Override
    protected String getProcessDefId() {
        return "obj_b10ba8b163604fae9a2d9f3b7c430529";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_c10e4710712444ff8b0f06ec81ff24fd";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        String code=mainData.getString("suCode");
        String type =mainData.getString("yzlx");
        return code+type;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("suCode");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_SealUseManage";
    }


    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("suCode");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        //查询实施单位是康益海洋的公司
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_SealUseManage  WHERE orcode = '1040'  ");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
